#coding=utf-8

import re
from selenium import webdriver
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select


class Web:
    def __init__(self):
        self.br = 'gc'
        self.driver: webdriver.Chrome
        self.relations = {}

    def open_browser(self, br = 'gc'):
        if br == None or br == '':
            self.driver = webdriver.Chrome()
        elif br == 'ff':
            self.driver = webdriver.Firefox()
        elif br == 'ie':
            self.driver = webdriver.Ie()
        else:
            self.driver = webdriver.Chrome()

        self.driver.maximize_window()
        self.driver.implicitly_wait(15)

    def __find_element(self, locator: str = ''):
        if locator is None or locator == '':
            return None
        elif locator.startswith('/'):
            return self.driver.find_element_by_xpath(locator)
        elif locator.startswith('#') or locator.__contains__('>'):
            return self.driver.find_element_by_css_selector(locator)
        else:
            return self.driver.find_element_by_id(locator)

    def click(self, locator):
        locator = self.__get_relations(locator)
        element = self.__find_element(locator)
        element.click()

    def input(self, locator, value):
        element = self.__find_element(locator)
        element.send_keys(value)

    def get_url(self, url):
        self.driver.get(url)

    def click_js(self, locator):
        element = self.__find_element(locator)
        self.driver.execute_script("arguments[0].click()", element)

    def sleep(self, t: str = '1'):
        t = int(t)
        sleep(t)

    def get_title(self):
        title = self.driver.title
        print(title)

    def get_text(self, locator):
        element = self.__find_element(locator)
        text = element.text
        print(text)

    def quit(self):
        self.driver.quit()

    def into_iframe(self, locator):
        element = self.__find_element(locator)
        self.driver.switch_to.frame(element)

    def out_iframe(self):
        self.driver.switch_to.default_content()

    def select(self, locator, value):
        element = self.__find_element(locator)
        select = Select(element)
        try:
            int(value)
            select.select_by_value(value)
        except:
            select.select_by_visible_text(value)

    def move_to(self, locator):
        element = self.__find_element(locator)
        action = ActionChains(self.driver)
        action.move_to_element(element).perform()

    def get_relation_text(self, locator, reg):
        element = self.__find_element(locator)
        text = element.text
        if reg:
            text = re.findall(reg, text)[0]
        self.relations['text'] = text
        print(text)
        print(self.relations)

    def __get_relations(self, params: str = ''):
        if params:
            for key in self.relations:
                params = params.replace('{' + key + '}', str(self.relations.get(key)))
        return params

    def switch_win(self):
        handles = self.driver.window_handles
        print(handles)
        self.driver.close()
        self.driver.switch_to.window(handles[1])